package com.qingcloud.adminbackend.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.qingcloud.adminbackend.entity.product.ProductDTO;
import com.qingcloud.adminbackend.entity.product.ProductTypeDTO;
import com.qingcloud.adminbackend.entity.product.SysProductType;
import com.qingcloud.adminbackend.entity.sensor.SysSensorFactorConfigDTO;
import com.qingcloud.adminbackend.mapper.SysProductMapper;
import com.qingcloud.adminbackend.mapper.SysSensorFactorConfigMapper;
import com.qingcloud.adminbackend.service.ISysProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 产品
 */
@Service
public class SysProductServiceImpl implements ISysProductService {

    @Resource
    private SysProductMapper sysProductMapper;

    @Resource
    private SysSensorFactorConfigMapper sysSensorFactorConfigMapper;

    /**
     * 全部产品
     *
     * @return
     */
    @Override
    public List<ProductDTO> products() {
        return sysProductMapper.getProducts();
    }

    /**
     * 产品类型
     *
     * @param productId
     * @return
     */
    @Override
    public List<ProductTypeDTO> productTypes(String productId) {
        List<ProductTypeDTO> result = new ArrayList<>();
        List<SysProductType> tempList = sysProductMapper.getProductTypes(productId);
        tempList.forEach(t -> {
            ProductTypeDTO pt = new ProductTypeDTO();
            pt.setId(t.getId());
            pt.setProductType(t.getProductType());
            String sensorFactors = t.getSensorFactors();
            String[] factorIds = sensorFactors.split(",");
            List<SysSensorFactorConfigDTO> sfs = new ArrayList<>();
            if (!ArrayUtil.isEmpty(factorIds)) {
                sfs = sysSensorFactorConfigMapper.findByIds(Arrays.asList(factorIds));
            }
            pt.setSensorFactors(sfs);
            result.add(pt);
        });
        return result;
    }

    @Override
    public List<ProductTypeDTO> productTypies(String productId) {
        List<ProductTypeDTO> result = new ArrayList<>();
        List<SysProductType> tempList = sysProductMapper.productTypies(productId);
        tempList.forEach(t -> {
            ProductTypeDTO pt = new ProductTypeDTO();
            pt.setId(t.getId());
            pt.setProductType(t.getProductType());
            String sensorFactors = t.getSensorFactors();
            String[] factorIds = sensorFactors.split(",");
            List<SysSensorFactorConfigDTO> sfs = new ArrayList<>();
            //查询可检测因子
            if (!ArrayUtil.isEmpty(factorIds)) {
                sfs = sysSensorFactorConfigMapper.findByIds(Arrays.asList(factorIds));
            }
            pt.setSensorFactors(sfs);
            result.add(pt);
        });
        return result;
    }
}
